 package com.tnl.hunter.webchat.utils;
 
 import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Repository;

import com.biz.system.config.WebchatServiceConstant;
 
 @Repository
 public class MailService
 {
   private static final Logger logger = LoggerFactory.getLogger(MailService.class);
   
   public static String sendMail(String to, String content)
   {
     JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
     
     senderImpl.setHost(WebchatServiceConstant.MAILHOST);
     
 
     //SimpleMailMessage mailMessage = new SimpleMailMessage();

     Properties prop = new Properties();
     prop.put(" mail.smtp.auth ", " true ");
     prop.put(" mail.smtp.timeout ", " 25000 ");
     senderImpl.setJavaMailProperties(prop);
     
     senderImpl.setUsername(WebchatServiceConstant.MAILUID);
     senderImpl.setPassword(WebchatServiceConstant.MAILKEY);
   
     
     MimeMessage mailMessage= senderImpl.createMimeMessage();
     
 
     try {
    	 mailMessage.setRecipients(javax.mail.Message.RecipientType.TO, to);
	
	     mailMessage.setFrom(WebchatServiceConstant.MAILUID);
	     mailMessage.setSubject(WebchatServiceConstant.MAILSUBJECT);
	     StringBuffer messageText=new StringBuffer();//内容以html格式发送,防止被当成垃圾邮件
	     messageText.append(WebchatServiceConstant.MAILCONTENT+ content);
	     mailMessage.setContent(messageText.toString(), "text/html;charset=gb2312");
	  
	     senderImpl.send(mailMessage);
	     logger.info(" 邮件发送成功.. ");
     } catch (MessagingException e) {
 		// TODO Auto-generated catch block
 		e.printStackTrace();
 	}
    
     return "Success";
   }
 }


